import datetime
from rest_framework import serializers
from rest_framework import status
from rest_framework.response import Response
from rest_framework_jwt.settings import api_settings
from django.utils import timezone

from users.serializers import UserPartInfoSerializer
from .models import Columns, Headlines, HeadlineComments, OtherComments


class ColumnsSerialier(serializers.ModelSerializer):
    """
    专栏分类序列化器
    """

    class Meta:
        model = Columns
        # fields = "__all__"
        exclude = ('create_time',)


class HeadlineListSerialier(serializers.ModelSerializer):
    """
    专栏分类列表序列化器
    """
    user = UserPartInfoSerializer()

    class Meta:
        model = Headlines
        fields = ['id', 'title', 'user', 'create_time', 'content', 'is_vip']


class SendHeadlineSerializer(serializers.ModelSerializer):
    """
    发布头条序列化器
    """
    class Meta:
        model = Headlines
        fields = ('column', 'title', 'content', 'user')
        extra_kwargs = {
            # 校验标题
            'title': {
                'min_length': 5,
                'max_length': 30,
                'error_messages': {
                    'min_length': '仅允许5-30个字符的标题',
                    'max_length': '仅允许5-30个字符的标题',
                }
            },
            # 校验内容
            'content': {
                'min_length': 5,
                'max_length': 3000,
                'error_messages': {
                    'min_length': '仅允许5-3000个字符的内容',
                    'max_length': '仅允许5-3000个字符的内容',
                }
            },
        }

    def create(self, validated_data):
        """
        生成时间
        """
        # default_img
        # is_active
        # is_delete
        # clicks
        # live_count
        # is_vip
        # validated_data['is_active'] = "CodeDragon_" + validated_data['mobile']
        time_utc = datetime.datetime.now()
        time_now = (time_utc + datetime.timedelta(hours=8))
        validated_data['create_time'] = time_now
        headline = super().create(validated_data)

        return headline


class HeadlineCommentsSerializer(serializers.ModelSerializer):
    """
    查看头条详情评论序列化器
    """
    # comment_content = serializers.CharField(min_length=10,label='评论内容')
    # headline = HeadlineListSerialier()
    user = UserPartInfoSerializer()
    class Meta:
        model = HeadlineComments
        fields = ['id','user','headline','create_time','comment_content']


class SubCommontSerializer(serializers.ModelSerializer):
    """
    头条详情发表主评论序列化器
    """
    class Meta:
        model = HeadlineComments
        fields = ['id','user','headline','comment_content']
        extra_kwargs = {
            # 校验
            'comment_content': {
                'min_length': 10,
                'max_length': 256,
                'error_messages': {
                    'min_length': '仅允许10-300个字符的内容',
                    'max_length': '仅允许10-256个字符的内容',
                }
            },
        }

    def create(self, validated_data):
        """
        生成时间
        """
        time_utc = datetime.datetime.now()
        time_now = (time_utc + datetime.timedelta(hours=8))
        validated_data['create_time'] = time_now
        headline_comment = super().create(validated_data)



        return headline_comment


class OtherCommentsSerializer(serializers.ModelSerializer):
    """
    主评论查看子评论序列化器
    """
    # comment_content = serializers.CharField(min_length=10,label='评论内容')
    # headline = HeadlineListSerialier()
    user = UserPartInfoSerializer()
    headline_comment = HeadlineCommentsSerializer()
    class Meta:
        model = OtherComments
        fields = ['id','user','headline_comment','comment_content']



class OtherSubCommentSerializer(serializers.ModelSerializer):
    """
    子评论回复主评论序列化器
    """
    class Meta:
        model = OtherComments
        fields = ['id','user', 'headline_comment','comment_content', 'parent']
        extra_kwargs = {
            # 校验
            'comment_content': {
                'min_length': 10,
                'max_length': 256,
                'error_messages': {
                    'min_length': '仅允许10-300个字符的内容',
                    'max_length': '仅允许10-256个字符的内容',
                }
            },
        }

    def create(self, validated_data):
        """
        生成时间
        """
        time_utc = datetime.datetime.now()
        time_now = (time_utc + datetime.timedelta(hours=8))
        validated_data['create_time'] = time_now
        others_comment = super().create(validated_data)


        return others_comment


class GetHotHeadlineSerializer(serializers.ModelSerializer):
    user = UserPartInfoSerializer()
    class Meta:
        model = Headlines
        fields = ['id', 'title', 'user', 'create_time', 'content', 'is_vip']